package com.yaorange.jk.service.impl;

import com.yaorange.jk.dao.BaseDao;
import com.yaorange.jk.entity.Contract;
import com.yaorange.jk.entity.ExtCproduct;
import com.yaorange.jk.service.ExtCproductService;
import com.yaorange.jk.service.ContractService;
import com.yaorange.jk.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author coach tam
 * @date 2017/11/1
 */
@Service("extCproductService")
@Transactional
public class ExtCproductServiceImpl implements ExtCproductService {


    @Autowired
    private BaseDao<ExtCproduct,String> extCproductDao;


    @Autowired
    private ContractService contractService;
    @Override
    @Transactional(readOnly = true)
    public Pagination findPage(int pageNo, int pageSize, String id) {
        return extCproductDao.pageByHql("from ExtCproduct cp where cp.contractProduct.id = ?",pageNo,pageSize,id);
    }


    @Override
    public List<ExtCproduct> findAll() {
        return extCproductDao.getListByHQL("from ExtCproduct");
    }

    @Override
    public void save(ExtCproduct extCproduct) {
        //计算总金额
        Double amount = extCproduct.getPrice()*extCproduct.getCnumber();
        extCproduct.setAmount(amount);


        //更新合同总金额
        Contract contract=contractService.findById(extCproduct.getContractProduct().getContract().getId());
        //总金额=老总金额+附件总金额
        Double totalAmount= contract.getTotalAmount() + amount;
        contract.setTotalAmount(totalAmount);

        contractService.update(contract);
        extCproductDao.save(extCproduct);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.get(ExtCproduct.class,id);
    }

    @Override
    public void update(ExtCproduct model) {
        //更新货物总金额
        Double amount = model.getPrice()*model.getCnumber();
        model.setAmount(amount);

        //根据货物id货物 货物老数据
        ExtCproduct oldCp = extCproductDao.get(ExtCproduct.class,model.getId());


        //更新合同总金额
        Contract contract=contractService.findById(model.getContractProduct().getContract().getId());
        //总金额=老总金额-货物老金额+货物新金额
        //货物老金额
        Double oldAmount= oldCp.getAmount();
        Double totalAmount= contract.getTotalAmount() - oldAmount + amount;
        contract.setTotalAmount(totalAmount);

        contractService.update(contract);

        extCproductDao.merge(model);
    }

    @Override
    public void deleteByIds(String[] ids) {
        for (String id :ids)
        {
            //根据货物id获取货物
            ExtCproduct cp = extCproductDao.get(ExtCproduct.class,id);

            //获取合同
            Contract contract = cp.getContractProduct().getContract();
            //更新合同总金额 = 总金额 - 当前附件金额
            Double totalAmout = contract.getTotalAmount()-cp.getAmount();
            contract.setTotalAmount(totalAmout);

            contractService.update(contract);

            extCproductDao.deleteById(ExtCproduct.class,id);
        }
    }
}
